const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    mobile:"",
    pwd:"",
    array: [],
    objectArray: [
      
    ],
    index: 0,
    sendmsg: "sendmsg",
    getmsg: "获取验证码",
  },
  onShow:function(){
    
    var token =  wx.getStorageSync('token');
    var mem_id = wx.getStorageSync('mem_id');
    var role_id = wx.getStorageSync('role_id');
    // wx.setStorageSync('api_root', 'https://p.t.fangx999.com/')
    // App.api_root = 'https://p.t.fangx999.com/';
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // wx.setStorageSync('api_root', 'https://p.t.fangx999.com/')
    // App.api_root = 'https://p.t.fangx999.com/';
    //获取加盟商  用于选择
    this.getBusiness();
  },

  sendmessg: function (e) {
    var sendStatus = this.data.sendmsg;
    var mobile = this.data.mobile;
    if(mobile == ''){
      App.showError('手机号不能为空');
      return false;
    }
    let reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(19[0-9]{1}))+\d{8})$/;
    if (!reg.test(mobile)) {
      App.showError('手机号不符合要求');
      return false;
    }
    if(sendStatus == 'sendmsgafter'){
      return false;
    }
    var _this = this;

    // wx.showLoading({
    //   title: '获取中...',
    // })
    wx.setStorageSync('api_root', 'https://p.t.fangx999.com/')
    App._post_form('login/sendmessg', {
      mobile: _this.data.mobile,
    }, function(result) {
      console.log(result);
      if(result.code == 0){
        //跑倒计时
        _this.runTime();
      }else{
        App.showError(result.msg);
      }
    }, false, function() {
      wx.hideLoading();
    });
     
  }, 

  runTime:function(){
    var timer=1;
     if (timer == 1) {
      timer = 0
      var that = this
      var time = 60
      that.setData({
        sendmsg: "sendmsgafter",
      })
      var inter = setInterval(function () {
      that.setData({
       getmsg: time + "s后重新发送",
      })
      time --
      if (time < 0) {
       timer = 1
       clearInterval(inter)
       that.setData({
       sendmsg: "sendmsg",
       getmsg: "获取短信验证码",
       })
      }
      }, 1000)
     }
  },

  //获取加盟商
  getBusiness:function(){
    var _this = this;
    App._post_fangx('business/getList', {}, function(result) {
      console.log(result)
      _this.setData({
        objectArray: result.data.list,
      })
      _this.setData({
        array: result.data.array,
      })
    });
  },

  //下拉选择
  bindPickerChange: function(e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    var index = e.detail.value;
    var url = this.data.objectArray[index].url;
    App.api_root = url;
    console.log(url);
    wx.setStorageSync('api_root', url);
    this.setData({
      index: e.detail.value
    })
    console.log(e)
  },
  //手机号赋值
  inputeidt:function(e){
    this.data.mobile = e.detail.value;
  },
  //密码赋值
  pwd:function(e){
    this.data.pwd = e.detail.value;
  },
  //登陆
  goto:function(){
      let _this = this;
      if (_this.data.mobile == ''){
        App.showError('手机号不能为空');
        return false;
      }
      let reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(19[0-9]{1}))+\d{8})$/;
      if (!reg.test(_this.data.mobile)) {
        App.showError('手机号不符合要求');
        return false;
      }
      if (_this.data.pwd == ''){
        App.showError('验证码不能为空');
        return false;
      }
      wx.showLoading({
        title: '登录中...',
      })
      wx.setStorageSync('api_root', 'https://p.t.fangx999.com/')
      App._post_form('login/loginAll', {
        mobile: _this.data.mobile,
        pwd: _this.data.pwd
      }, function(result) {

        // console.log(result);
        // return false;
        if(result.code == ''){
          App.showError(result.msg);
          return false;
        }
        // 记录token user_id
        wx.setStorageSync('token', result.data.token);
        wx.setStorageSync('mem_id', result.data.mem_id);
        wx.setStorageSync('role_id', result.data.role_id);
        wx.setStorageSync('is_shangji', result.data.is_shangji);
        wx.setStorageSync('site', result.data.site);
        wx.setStorageSync('api_root', result.data.site_domain);
        //如果是demo站点
        if(result.data.site == 'demo_site'){
          wx.reLaunch({
            url:result.data.url
          })
        }else{
          if(result.data.role_id == 7 || result.data.role_id == 8 || result.data.role_id == 10 ){
            wx.switchTab({
              url: result.data.url
            })
          }else{
            wx.reLaunch({
              url:result.data.url
            })
          }
        }
        
      }, false, function() {
        wx.hideLoading();
      });
  },

  //登陆
  gotoCode:function(){
    let _this = this;
    if (_this.data.mobile == ''){
      App.showError('手机号不能为空');
      return false;
    }
    let reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(19[0-9]{1}))+\d{8})$/;
    if (!reg.test(_this.data.mobile)) {
      App.showError('手机号不符合要求');
      return false;
    }
    if (_this.data.pwd == ''){
      App.showError('验证码不能为空');
      return false;
    }
    wx.showLoading({
      title: '登录中...',
    })
    wx.setStorageSync('api_root', 'https://p.t.fangx999.com/')
    // loginAll
    App._post_form('login/loginAll', {
    // App._post_form('login/loginCode', {
      mobile: _this.data.mobile,
      pwd: _this.data.pwd
    }, function(result) {

      console.log(result);
      // return false;
      if(result.code == ''){
        App.showError(result.msg);
        return false;
      }
      // 记录token user_id
      wx.setStorageSync('token', result.data.token);
      wx.setStorageSync('mem_id', result.data.mem_id);
      wx.setStorageSync('role_id', result.data.role_id);
      wx.setStorageSync('is_shangji', result.data.is_shangji);
      wx.setStorageSync('site', result.data.site);
      wx.setStorageSync('api_root', result.data.site_domain);
      //如果是demo站点
      if(result.data.site == 'demo_site'){
        wx.reLaunch({
          url:result.data.url
        })
      }else{
        if(result.data.role_id == 7 || result.data.role_id == 8 || result.data.role_id == 10 ){
          wx.switchTab({
            url: result.data.url
          })
        }else{
          wx.reLaunch({
            url:result.data.url
          })
        }
      }
      
    }, false, function() {
      wx.hideLoading();
    });
},


  /**
   * 授权登录
   */
  authorLogin: function(e) {
    let _this = this;
    if (e.detail.errMsg !== 'getUserInfo:ok') {
      return false;
    }
    console.log(_this.data.mobile);
    return false;

    
  },

  /**
   * 授权成功 跳转回原页面
   */
  navigateBack: function() {
    let currentPage = wx.getStorageSync('currentPage');
    if (currentPage.route == 'pages/goods/index') {
      wx.redirectTo({
        url: '/' + currentPage.route + '?' + App.urlEncode(currentPage.options)
      });
    } else {
      wx.navigateBack();
    }
  }
})